Global class CXGCampaignStart{ 
     
     //Class Description:Check the active Checkbox on Campaign and populate the Start Date to Date.Today()
     //Check for the Number of Control Group Members specified on the Campaign and Assign them to
     //Control Group queue.Remaining Members are assigned to Agent Qeuue
     public static QueueSobject agentqueueId = [Select QueueId from QueueSobject where Queue.Name ='Agent Queue' LIMIT 1];
     public static QueueSobject controlgrpQueueId = [Select QueueId from QueueSobject where Queue.Name ='Control Group Queue' Limit 1];
     public static QueueSobject holdingQueueId=[Select QueueId from QueueSobject where Queue.Name ='Holding Queue' Limit 1];
     
      WebService static void CampaignStart(String campaignId){ 
      
           List<CXG_Campaign_Accounts__c> cmlst= New List<CXG_Campaign_Accounts__c>();
           List<CXG_Campaign_Accounts__c> cmnewlist= New List<CXG_Campaign_Accounts__c>();
           List<CXG_Campaign_Accounts__c> cmlstcgrp= New List<CXG_Campaign_Accounts__c>();
           
           Campaign cam=[Select Number_of_Control_Group_Members__c from Campaign where Id=:campaignId];
       
           System.Debug('LIMIT'+cam.Number_of_Control_Group_Members__c);
       
           ID CxGcampaignmemberrecordtypeID = RecordTypeHelper.GetRecordTypeIdByDevName('CXG_Campaign_Accounts__c', 'Campaign_Member');       
           Integer limitvalue=Integer.ValueOf(cam.Number_of_Control_Group_Members__c);
       
           List<CXG_Campaign_Accounts__c> cmfinallist=New List<CXG_Campaign_Accounts__c>();
           List <CXG_Campaign_Accounts__c> cmclosedlst=New List<CXG_Campaign_Accounts__c>();
           List<CXG_Campaign_Accounts__c> acctcontrolgrplist= New List<CXG_Campaign_Accounts__c>();
           List<CXG_Campaign_Accounts__c> cmcontrolgrplist= New List<CXG_Campaign_Accounts__c>();
           cmlst=[Select Id from CXG_Campaign_Accounts__c Where Campaign__c=:campaignId AND Control_Group_Member__c =False AND recordtypeid =: CxGcampaignmemberrecordtypeID  AND Campaign_Paused__c =False AND Customer__r.CXG_Do_Not_Call__c =False AND Customer__r.Line_Disconnected_Fax_Line__c=False]; 
           String campMemberQueryString=null;     
       
           campMemberQueryString = 'Select Id from CXG_Campaign_Accounts__c where recordtypeid =: CxGcampaignmemberrecordtypeID  and Customer__r.Control_Group__c=True and Control_Group_Member__c = False and Campaign__C =\'' + campaignId + '\' limit '+ limitvalue;           
           acctcontrolgrplist=Database.Query(campMemberQueryString);    
           //QueueSobject agentqueueId = [Select QueueId from QueueSobject where Queue.Name ='Agent Queue' LIMIT 1];
           //QueueSobject controlgrpQueueId=[Select QueueId from QueueSobject where Queue.Name ='Control Group Queue' Limit 1]; 
               for(CXG_Campaign_Accounts__c cm:cmlst)
               {
                cm.OwnerId =agentqueueId.QueueId; //set the value for OwnerId to Default 'Agent Queue' 
                cm.Status__c='New';//set the value of status to 'New' 
                cm.IscampaignActive__c=True; 
                cmfinallist.add(cm);
               }    
               update cmfinallist;
               for(CXG_Campaign_Accounts__c cm:acctcontrolgrplist)
               {
                cm.Control_Group_Member__c=true; 
                cm.OwnerId=controlgrpQueueId.QueueId; 
                cm.IscampaignActive__c=True; 
                cmnewlist.add(cm);
               }
               Update cmnewlist;
           System.Debug('LIST******************1'+cmnewlist);
           Integer remaning= limitvalue - cmnewlist.size();
           System.Debug('REMaining'+remaning);
           String campMemberQueryStringnew=null;
           campMemberQueryStringnew = 'Select Id from CXG_Campaign_Accounts__c where recordtypeid =: CxGcampaignmemberrecordtypeID  AND Customer__r.Line_Disconnected_Fax_Line__c=False AND Customer__r.CXG_Do_Not_Call__c =False  AND Control_Group_Member__c = False and Campaign__C =\'' + campaignId + '\' limit '+ remaning;           
           cmcontrolgrplist=Database.Query(campMemberQueryStringnew);
               for(CXG_Campaign_Accounts__c cm:cmcontrolgrplist)
               {
                cm.Control_Group_Member__c=True; 
                cm.OwnerId=controlgrpQueueId.QueueId; 
                cm.IscampaignActive__c=True; 
                cmlstcgrp.add(cm);
               }
               Update cmlstcgrp;
           System.Debug('LIST*****************1'+cmlstcgrp);
               for(CXG_Campaign_Accounts__c cm:[Select Id from CXG_Campaign_Accounts__c Where Campaign__c=:campaignId AND recordtypeid =: CxGcampaignmemberrecordtypeID   AND Control_Group_Member__c = False AND Campaign_Paused__c =False  AND Customer__r.CXG_Do_Not_Call__c =True AND Customer__r.Control_Group__c =False])
               {
                cm.OwnerId =agentqueueId.QueueId; //set the value for OwnerId to Default 'Agent Queue' 
                cm.Status__c='Closed';//set the value of status to 'New' 
                cm.IscampaignActive__c=False; 
                cmclosedlst.add(cm);
               }
           update cmclosedlst;
           System.Debug('LIST**************************3'+cmclosedlst);
           System.Debug('LIST4*******************4'+cmfinallist);
         cam.StartDate=Date.Today();
         cam.IsActive=True;
         update cam;
    }
    static testMethod void testCmapignStart()
    {
     Campaign c= New Campaign();
     c.Name='TestCampaign';
     c.StartDate=Date.Today();
     c.IsActive=true;
     c.Number_of_Control_Group_Members__c=20; 
     Insert c;
     //QueueSobject agentqueueId = [Select QueueId from QueueSobject where Queue.Name ='Agent Queue' LIMIT 1];
     //QueueSobject controlgrpQueueId=[Select QueueId from QueueSobject where Queue.Name ='Control Group Queue' Limit 1]; 
     Account acct= New Account();
     acct.Name='TestStart';
     Insert acct;
     CXG_Campaign_Accounts__c cm = New CXG_Campaign_Accounts__c();
     cm.Campaign__c=c.Id;
     cm.Customer__c=acct.id;  
     cm.Status__c='New';
     Insert cm;
     cm.IscampaignActive__c=True;
     cm.OwnerId =agentqueueId.QueueId;
     update cm;
     CXGCampaignStart.CampaignStart(c.Id);
     
    } 
    
 }